/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.web.controller;

import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.bean.DataContainer;
import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.TemplateManager;
import com.genia.toolbox.projects.toolbox_basics_project.web.command.TemplateCommand;
import com.genia.toolbox.web.context.WebContext;
import com.genia.toolbox.web.mvc.controller.AbstractDownloadController;

/**
 * Download controller for file that are in a template bundle.
 */
public abstract class AbstractTemplateController
    extends AbstractDownloadController<TemplateCommand>
{

  /**
   * the {@link TemplateManager} to use.
   */
  private TemplateManager templateManager;

  /**
   * the {@link WebContext} to use.
   */
  private WebContext webContext;



  /**
   * returns the {@link DataContainer} to display.
   * 
   * @param templateCommand
   *          the form containing the parameter of the request
   * @return the {@link DataContainer} to display or <code>null</code> if no
   *         such object exists
   * @throws BundledException
   *           when an error occured
   * @see com.genia.toolbox.web.mvc.controller.AbstractDownloadController#getDataContainer(java.lang.Object)
   */
  @Override
  public DataContainer getDataContainer(TemplateCommand templateCommand)
      throws BundledException
  {
    String path = getWebContext().getRelativePath();
    if (path.startsWith(getControllerPath())) {
      path = path.substring(getControllerPath().length());
    }
    if (!StringUtils.hasText(path)) {
      path = null;
    }

    return getDataContainer(templateCommand, path);
  }



  /**
   * getter for the templateManager property.
   * 
   * @return the templateManager
   */
  public TemplateManager getTemplateManager()
  {
    return templateManager;
  }



  /**
   * getter for the webContext property.
   * 
   * @return the webContext
   */
  public WebContext getWebContext()
  {
    return webContext;
  }



  /**
   * setter for the templateManager property.
   * 
   * @param templateManager
   *          the templateManager to set
   */
  public void setTemplateManager(TemplateManager templateManager)
  {
    this.templateManager = templateManager;
  }



  /**
   * setter for the webContext property.
   * 
   * @param webContext
   *          the webContext to set
   */
  public void setWebContext(WebContext webContext)
  {
    this.webContext = webContext;
  }



  /**
   * returns the mapping path of this controller.
   * 
   * @return the mapping path of this controller
   */
  protected abstract String getControllerPath();



  /**
   * returns the {@link DataContainer} that must be displayed depending on a
   * {@link TemplateCommand} and a path.
   * 
   * @param templateCommand
   *          the {@link TemplateCommand} describing the template to use
   * @param path
   *          the path of the object to display
   * @return the {@link DataContainer} that must be displayed depending on a
   *         {@link TemplateCommand} and a path
   * @throws TechnicalException
   *           if an error occured
   */
  protected abstract DataContainer getDataContainer(final TemplateCommand templateCommand, final String path)
      throws TechnicalException;

}
